package redis_springboot.demo.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author： pang-yun
 * @date： 2021-12-23 10:03
 * 功能目的 ：用于  mq 的配置类
 */
@Configuration
public class RabbitMqConfig {
    //--------------------------------------------------常量-----------------------

    // 普通交换机 与 死信交换机
    private static final String NORMAL_EXCHANGE = "X";
    private static final String DEAD_EXCHANGE = "Y";
    // 两个 普通队列 与 一个死信队列
    private static final String QUEUE_A = "QA";
    private static final String QUEUE_B = "QB";
    private static final String QUEUE_DEAD = "QD";
    private static final String QUEUE_C = "QC";
    // 路由
    private static final String X_QA = "XA";
    private static final String X_QB = "XB";
    private static final String DEAD_ROUTE = "YD";
    private static final String X_QC = "XC";


    // --------------------------------------声明交换机-----------------------------
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(NORMAL_EXCHANGE);
    }

    @Bean("deadExchange")
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_EXCHANGE);
    }

    //------------------------------------------------------ 声明 普通队列 死信队列----------------------------
    @Bean("queueA")
    public Queue queueA() {
        Map<String, Object> arguments = new HashMap<>();
        // 声明 当前队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        // 声明 当前队列绑定的路由
        arguments.put("x-dead-letter-routing-key", DEAD_ROUTE);
        // 声明 当前队列的过期时间
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    @Bean("queueB")
    public Queue queueB() {
        Map<String, Object> arguments = new HashMap<>();
        // 声明 当前队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        // 声明 当前队列绑定的路由
        arguments.put("x-dead-letter-routing-key", DEAD_ROUTE);
        // 声明 当前队列的过期时间
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    //声明死信队列
    @Bean("deadQueue")
    public Queue deadQueue() {
        return QueueBuilder.durable(QUEUE_DEAD).build();
    }

    //优化延时队列
    @Bean("delayQueue")
    public Queue delayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        // 声明 当前队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        // 声明 当前队列绑定的路由
        arguments.put("x-dead-letter-routing-key", DEAD_ROUTE);
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }

    //-----------------建立 交换机与队列的绑定关系----------------------------
    @Bean
    public Binding queueABindingX(@Qualifier("xExchange") DirectExchange exchange,
                                  @Qualifier("queueA") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(X_QA);

    }

    @Bean
    public Binding queueBBindingX(@Qualifier("xExchange") DirectExchange exchange,
                                  @Qualifier("queueB") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(X_QB);
    }

    //死信队列绑定
    @Bean
    public Binding queueDeadBindingDead(@Qualifier("deadExchange") DirectExchange exchange,
                                        @Qualifier("deadQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_ROUTE);
    }

    // 优化的延时队列绑定交换机
    @Bean
    public Binding delayBindingX(@Qualifier("xExchange") DirectExchange exchange,
                                 @Qualifier("delayQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(X_QC);
    }
    //--------------------------------------------------------

}
